package com.cslg.ac.demo.controller.user;


import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.entity.user.User;
import com.cslg.ac.demo.entity.user.team.TeamRecruit;
import com.cslg.ac.demo.service.user.TeamRecruitService;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@PropertySource(value = {"classpath:user.properties"},encoding="utf-8")
public class TeamReruitController {

    @Autowired
    TeamRecruitService teamRecruitService;

    @Value("${contentLength}")
    int contentLength;
    @Value("${placeLength}")
    int placeLength;


    @RequestMapping(value = "/recruit/getByTeam")
    public void getRecruitByTeamId(@RequestParam int teamId, @RequestParam int offset,
                                   @RequestParam int num, PrintWriter out){
        Map<String, Object> param = new HashMap<>(1);
        Integer[] ids;
        if(offset < 1 || num < 1){
            ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
            return;
        }
        try {
            ids = teamRecruitService.selectByTeam(teamId, offset - 1, num);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
            return;
        }
        if(ids == null || ids.length < 1){
            ids = null;
        }
        param.put("ids", ids);
        ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);

    }

    @RequestMapping(value = "/recruit/getInfo")
    public void getInfo(@RequestParam int recruitId, PrintWriter out){
        Map<String, Object> param = new HashMap<>(1);
        TeamRecruit teamRecruit;
        try {
            teamRecruit = teamRecruitService.selectById(recruitId);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
            return;
        }
        param.put("recruit", teamRecruit);
        ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
    }




    @RequestMapping(value = "/recruit/add")
    public void addRecruit(TeamRecruit teamRecruit, HttpSession session, PrintWriter out){
        User user = (User)session.getAttribute("user");
        int teamId = user.getId();
        Integer recruitId;
        if(!teamRecruit.insertValid(contentLength, placeLength)){
            ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
            return;
        }
        try {
            recruitId = teamRecruitService.insert(teamRecruit, teamId);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
            return;
        }
        Map param = new HashMap(1);
        param.put("id", recruitId);
        ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
        return;
    }
}
